﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Business;
using System.Data;
using System.Data.SqlClient;

public partial class User_Shares_ExcelOutputSelectPage : System.Web.UI.Page
{
    private int _TotalPages;
    public int TotalPages
    {
        get { return _TotalPages; }
        set
        {
            _TotalPages = value;
            Session["TotalPages"] = value;
        }
    }

    private int _CurrentPage;
    public int CurrentPage
    {
        get { return _CurrentPage; }
        set
        {
            Session["PageIndex"] = value;
            _CurrentPage = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        if (thisUser.Identity.IsAuthenticated)
        {
            
            if (!Page.IsPostBack)
            {
                Session["ContractIds"] = null;
                Session["ContractIds"] = null;
                Session["TotalPages"] = Business.ProjectDetails.GetTotalPageCountOfContracts(GridViewProjects.PageSize, true, true).ToString();
                Session["PageIndex"] = 0;
                CurrentPage = Convert.ToInt32(Session["PageIndex"]);
                TotalPages = Convert.ToInt32(Session["TotalPages"]);
                PopulateGrid(thisUser);
            }
            CurrentPage = Convert.ToInt32(Session["PageIndex"]);
            TotalPages = Convert.ToInt32(Session["TotalPages"]);

                if (LinkButtonPrevious != null)
                {
                    if (CurrentPage <= 1)
                    {
                        LinkButtonPrevious.Enabled = false;
                    }
                    else
                    {
                        LinkButtonPrevious.Enabled = true;
                    }
                }
                if (LinkButtonNext != null)
                {
                    if (CurrentPage >= TotalPages)
                    {
                        LinkButtonNext.Enabled = false;
                    }
                    else
                    {
                        LinkButtonNext.Enabled = true;
                    }
                }
            
        }
    }

    public void PopulateGrid(System.Security.Principal.IPrincipal thisUser)
    {
        DropDownList downList = (LoginViewSearch.FindControl("DropDownListSort") as DropDownList);
        RadioButton radioButtonAsc = (LoginViewSearch.FindControl("RadioButtonAsc") as RadioButton);
        RadioButton radioButtonDec = (LoginViewSearch.FindControl("RadioButtonDec") as RadioButton);
        DropDownList dropDownList = LoginViewSearch.FindControl("DropDownListCount") as DropDownList;
        if ( dropDownList != null )
            GridViewProjects.PageSize = Convert.ToInt32( dropDownList.SelectedValue );
        if (thisUser.Identity.IsAuthenticated)
        {
            if (thisUser.IsInRole("admin")  ||  thisUser.IsInRole("employer") || thisUser.IsInRole("edituser") || thisUser.IsInRole("viewuser"))
            {
                bool advisorSearch = (LoginViewSearch.FindControl("CheckBoxAdvisor") as RadioButton).Checked;
                bool surveyorSearch = (LoginViewSearch.FindControl("CheckBoxSurveyor") as RadioButton).Checked;
                List<Business.GridContractType> dataSource = new List<GridContractType>();

                dataSource = Business.ProjectDetails.GetProjectAllPaged(GridViewProjects.PageSize, Convert.ToInt32(Session["PageIndex"]),
                                                                                         advisorSearch, surveyorSearch);
                if (radioButtonAsc != null)
                    if (radioButtonAsc.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderBy(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderBy(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderBy(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderBy(project => project.City).ToList();
                                    break;
                            }
                    }
                if (radioButtonDec != null)
                    if (radioButtonDec.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderByDescending(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderByDescending(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderByDescending(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderByDescending(project => project.City).ToList();
                                    break;
                            }
                    }
                GridViewProjects.DataSource = dataSource;
                Session["TotalPages"] =
                    Business.ProjectDetails.GetTotalPageCountOfContracts(GridViewProjects.PageSize, advisorSearch, surveyorSearch).ToString();
            }
            else if(thisUser.IsInRole("fourthfactor"))
            {
                bool advisorSearch = (LoginViewSearch.FindControl("CheckBoxAdvisor") as RadioButton).Checked;
                bool surveyorSearch = (LoginViewSearch.FindControl("CheckBoxSurveyor") as RadioButton).Checked;
                List<Business.GridContractType> dataSource = new List<GridContractType>();

                dataSource = Business.ProjectDetails.GetProjectAllPagedFourthFactor(Membership.GetUser().ProviderUserKey,GridViewProjects.PageSize, Convert.ToInt32(Session["PageIndex"]),
                                                                                         advisorSearch, surveyorSearch);
                if (radioButtonAsc != null)
                    if (radioButtonAsc.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderBy(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderBy(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderBy(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderBy(project => project.City).ToList();
                                    break;
                            }
                    }
                if (radioButtonDec != null)
                    if (radioButtonDec.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderByDescending(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderByDescending(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderByDescending(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderByDescending(project => project.City).ToList();
                                    break;
                            }
                    }
                GridViewProjects.DataSource = dataSource;
                Session["TotalPages"] =
                    Business.ProjectDetails.GetTotalPageCountOfContracts(GridViewProjects.PageSize, advisorSearch, surveyorSearch).ToString();
         
            }
            else if(thisUser.IsInRole("bonyadshahrestan"))
            {
                bool advisorSearch = (LoginViewSearch.FindControl("CheckBoxAdvisor") as RadioButton).Checked;
                bool surveyorSearch = (LoginViewSearch.FindControl("CheckBoxSurveyor") as RadioButton).Checked;
                List<Business.GridContractType> dataSource = new List<GridContractType>();
                IDataReader dr= DataAccess.TblUserDetail.FetchByParameter(DataAccess.TblUserDetail.Columns.UserId, Membership.GetUser().ProviderUserKey);
                dr.Read();
                Guid cityid=new Guid( dr["CityId"].ToString());
                dr.Close();
                dataSource = Business.ProjectDetails.GetProjectAllPagedshahrestan(cityid, GridViewProjects.PageSize, Convert.ToInt32(Session["PageIndex"]),
                                                                                         advisorSearch, surveyorSearch);
                if (radioButtonAsc != null)
                    if (radioButtonAsc.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderBy(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderBy(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderBy(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderBy(project => project.City).ToList();
                                    break;
                            }
                    }
                if (radioButtonDec != null)
                    if (radioButtonDec.Checked)
                    {
                        if (downList != null)
                            switch (Convert.ToInt32(downList.SelectedValue))
                            {
                                case 0:
                                    dataSource = dataSource.OrderByDescending(project => project.VillageName).ToList();
                                    break;
                                case 1:
                                    dataSource = dataSource.OrderByDescending(project => project.AdvisorFullName).ToList();
                                    break;
                                case 2:
                                    dataSource = dataSource.OrderByDescending(project => project.ContractNumber).ToList();
                                    break;
                                case 3:
                                    dataSource = dataSource.OrderByDescending(project => project.City).ToList();
                                    break;
                            }
                    }
                GridViewProjects.DataSource = dataSource;
                Session["TotalPages"] =
                    Business.ProjectDetails.GetTotalPageCountOfContracts(GridViewProjects.PageSize, advisorSearch, surveyorSearch).ToString();
         
            }
                else if(thisUser.IsInRole("advisor")
                      || thisUser.IsInRole("Surveyor"))
            {
                var userId = new Guid(Membership.GetUser().ProviderUserKey.ToString());
                GridViewProjects.AllowPaging = false;
                GridViewProjects.DataSource = Business.ProjectDetails.GetProjectByUserId(userId);
            }
            Page.DataBind();
        }
    }

    protected void LinkButtonNext_Click(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        if (LinkButtonNext.Enabled)
        {
            Session["PageIndex"] = (Convert.ToInt32(Session["PageIndex"])) + 1;
            PopulateGrid(thisUser);
        }

    }
    protected void LinkButtonPrevious_Click(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        if (LinkButtonPrevious.Enabled)
        {
            Session["PageIndex"] = (Convert.ToInt32(Session["PageIndex"])) - 1;
            PopulateGrid(thisUser);
        }
    }


    protected void LinkButtonFirst_Click(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        Session["PageIndex"] = 0;
        PopulateGrid(thisUser);
    }

    protected void LinkButtonLast_Click(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        Session["PageIndex"] = Convert.ToInt32(Session["TotalPages"]) - 1;
        PopulateGrid(thisUser);
    }
    protected void GridViewProjects_PreRender(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        if (thisUser.Identity.IsAuthenticated)
        {
            GridViewProjects.BottomPagerRow.Visible = true;
        }
    }
    protected void GridViewProjects_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowIndex % 2 == 0)
            {
                e.Row.BackColor = System.Drawing.Color.AliceBlue;
            }
            else
            {
                e.Row.BackColor = System.Drawing.Color.PowderBlue;
            }

            CheckBox checkBox = e.Row.FindControl("CheckBoxItemSelect") as CheckBox;
            if (checkBox != null)
            {
                // (Session["ContractIds"] as List<Guid>).Add( new Guid(GridViewProjects.SelectedDataKey.Value.ToString() ));
            }
        }
        if (e.Row.RowType == DataControlRowType.Pager)
        {
            e.Row.BackColor = System.Drawing.Color.FromArgb(0x61, 0x84, 0xC3);
            UpdatePanel updatePanel = e.Row.Cells[0].FindControl("UpdatePanelFooter") as UpdatePanel;
            Label lbl = updatePanel.FindControl("LabelPageNumber") as Label;
            if (lbl != null)
                lbl.Text = string.Format("{0} از {1} صفحه ", Convert.ToInt32(Session["PageIndex"].ToString())+1,  Session["TotalPages"]);
        }
    }
    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        PopulateGrid(thisUser);
    }

    protected void SessionContractIdsMaker()
    {

    }
    protected void CheckBoxItemSelect_CheckedChanged(object sender, EventArgs e)
    {
        List<Guid> list = new List<Guid>();
        if (Session["ContractIds"] != null)
        {
            list = Session["ContractIds"] as List<Guid>;
        }

        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow)checkbox.NamingContainer;
        if (checkbox.Checked == true)
        {
            row.BackColor = System.Drawing.Color.Red;
            list.Add(new Guid(GridViewProjects.DataKeys[row.RowIndex].Value.ToString()));
        }
        if (checkbox.Checked == false)
        {
            list.Remove(new Guid(GridViewProjects.DataKeys[row.RowIndex].Value.ToString()));
            if (row.RowIndex % 2 == 0)
            {
                row.BackColor = System.Drawing.Color.AliceBlue;
            }
            else
            {
                row.BackColor = System.Drawing.Color.PowderBlue;
            }
        }
        Session["ContractIds"] = list;
    }
    protected void ButtonExcel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/User/Shares/ExcelOutputMultiple.aspx");
    }


    protected void RadioButton_CheckedChanged(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        PopulateGrid(thisUser);
    }

    protected void DropDownChanged(object sender, EventArgs e)
    {
        var thisUser = HttpContext.Current.User;
        PopulateGrid(thisUser);
    }
    protected void GridViewProjects_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow &&
   (e.Row.RowState == DataControlRowState.Normal ||
    e.Row.RowState == DataControlRowState.Alternate))
        {
            CheckBox chkBxSelect = (CheckBox)e.Row.Cells[1].FindControl("CheckBoxItemSelect");
            CheckBox chkBxHeader = (CheckBox)this.GridViewProjects.HeaderRow.FindControl("CheckBoxSelectAll");
            chkBxSelect.Attributes["onclick"] = string.Format
                                                   (
                                                      "javascript:ChildClick(this,'{0}');",
                                                      chkBxHeader.ClientID
                                                   );
        }
    }
}